package com.shengyuan.manage_os.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.bean.UserInfo;
import com.shengyuan.manage_os.im.common.CCPAppManager;
import com.shengyuan.manage_os.im.ui.SDKCoreHelper;
import com.shengyuan.manage_os.im.ui.chatting.ChattingActivity;
import com.shengyuan.manage_os.sqlite.SQLiteTools;
import com.shengyuan.manage_os.view.CircleImageView;
import com.yuntongxun.ecsdk.ECDevice;

import java.util.ArrayList;
import java.util.List;

import oa.database.bean.Department;
import oa.database.bean.Employee;

public class SampleAdapter extends BaseExpandableListAdapter {

	private final Activity mContext;
	private final LayoutInflater mLayoutInflater;

	private Bitmap bitmap;
	private byte[] dataImage;

	private List<Department> mGroups = new ArrayList<>();
	private Intent chatIntent;



	public SampleAdapter(Activity context,List<Department> department) {
		mContext = context;
		this.mGroups = department;
		mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		chatIntent = new Intent(context, ChattingActivity.class);
	}
	
	@Override
	public int getGroupCount() {
		return mGroups.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mGroups.get(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		if(convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.sample_activity_list_group_item, parent, false);
		}

//		final ImageView image = (ImageView) convertView.findViewById(R.id.sample_activity_list_group_item_image);
//		image.setImageResource(mGroupDrawables[groupPosition]);

		final TextView text = (TextView) convertView.findViewById(R.id.sample_activity_list_group_item_text);
		text.setText(mGroups.get(groupPosition).getDepartName());
		
//		final ImageView expandedImage = (ImageView) convertView.findViewById(R.id.sample_activity_list_group_expanded_image);
//		final int resId = isExpanded ? R.mipmap._uidic_forms_item_arrow : R.mipmap.add_pressed;
//		expandedImage.setImageResource(resId);

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mGroups.get(groupPosition).getEmployeeList().size();
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return mGroups.get(groupPosition).getEmployeeList().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		if(convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.sample_activity_list_child_item, parent, false);
		}
		CircleImageView headImage = (CircleImageView) convertView.findViewById(R.id.hear);

		if(mGroups.get(groupPosition).getEmployeeList().get(childPosition).getHearImage() != null) {
			dataImage = Base64.decode(mGroups.get(groupPosition).getEmployeeList().get(childPosition).getHearImage(), Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(dataImage, 0, dataImage.length);
			headImage.setImageBitmap(bitmap);
		}

		
		final TextView text = (TextView) convertView.findViewById(R.id.sample_activity_list_child_item_text);
		text.setText(mGroups.get(groupPosition).getEmployeeList().get(childPosition).getUserName());
		text.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(SDKCoreHelper.getInstance().getState() == ECDevice.ECConnectState.CONNECT_SUCCESS) {
					Employee employee = mGroups.get(groupPosition).getEmployeeList().get(childPosition);
					String recipinets = employee.getUid() + "";
					String username = employee.getUserName();
					employee.setIsUsual(true);
					UserInfo.UID = recipinets;
					CCPAppManager.startChattingAction(getContent(), recipinets, username);
					SQLiteTools.upDataAssignEmployee(employee);
				}else {
					Toast.makeText(getContent(),"您还没有登录成功",Toast.LENGTH_LONG).show();
				}
			}
		});
		
		return convertView;
	}

	private Activity getContent(){
		return this.mContext;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
